<?php
/**
 * StatZone default Setting controller
 * 
 * @package    USS.StatZone
 * @subpackage Components
 * @link www.ugenix.co.za www.statzone.co.za
 * @license		Commercial
 * @author Salie Hendricks
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.controller' );

/**
 * @package		USS
 * @subpackage	StatZone
 */
class StatZoneControllerSetting extends JController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();
        $this->registerTask( 'apply',		'save' );
    }

    /**
	 * Display the settings
	 */
	function display()
	{
		global $mainframe;

		$db =& JFactory::getDBO();

		$query = 'SELECT s.id as id, s.name as name, s.value as value'
		. ' FROM #__statzone_settings AS s'
		;
        
       	$db->setQuery( $query);
        if (!$db->query())
		{
			$this->setRedirect( 'index.php?option=com_statzone' );
			return JError::raiseWarning( 500, $db->getErrorMsg() );
		}
       	$rows = $db->loadObjectList();
        
		$javascript		= 'onchange="document.adminForm.submit();"';
		
        require_once(JPATH_COMPONENT.DS.'views'.DS.'settings.php');
		StatZoneViewSettings::settings( $rows);
	}
    
	
	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save()
	{
    
        // Check for request forgeries
		JRequest::checkToken() or die( 'Invalid Token' );

		// Initialize variables
		$db =& JFactory::getDBO();
        
		$post	= JRequest::get( 'post' );
		
        $SectionID_value = $post['SectionID'];
        $ClubCategoryID_value = $post['ClubCategoryID'];
        $PlayerCategoryID_value = $post['PlayerCategoryID'];
        
        $sql = 'UPDATE #__statzone_settings SET value=\''. $SectionID_value .'\' WHERE name = \'SectionID\'';
        $db->execute($sql);
        
        $sql = 'UPDATE #__statzone_settings SET value=\''. $ClubCategoryID_value .'\' WHERE name = \'ClubCategoryID\'';
        $db->execute($sql);
        
        $sql = 'UPDATE #__statzone_settings SET value=\''. $PlayerCategoryID_value .'\' WHERE name = \'PlayerCategoryID\'';
        $db->execute($sql);       
        
        switch ($task)
		{
			case 'apply':
				$link = 'index.php?option=com_statzone&c=setting';
				break;

			case 'save':
			default:
				$link = 'index.php?option=com_statzone';
				break;
		}

		$this->setRedirect( $link, JText::_( 'Settings Saved' ) );
	}


	/**
	 * cancel editing a settings
	 * @return void
	 */
	function cancel()
	{
		$msg = JText::_( 'Operation Cancelled' );
		$this->setRedirect( 'index.php?option=com_statzone', $msg );
	}
}
?>
